<?php include 'studentHeader.html'; ?>
<h2>Zapisz</h2>

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

session_start();
$login=$_SESSION['login'];

		$link = pg_connect("host=labdb dbname=bd user=dt316119 password=x");       
		$dzis = date('Y-m-d');
		$query = "select * from cwiczenia where kod not in (select kod from ocena where indeks='$login' and status='zal') order by kod";
					
		$result = pg_query($link, $query);
		pg_close($link);
		$n = pg_num_rows($result);

		if($n == 0) { // jeśli brak cwiczen

			echo "<span>Nie możesz się zapisać!</span>";
			
		}
		else { // są cwiczenia

?>

<form action="zapisz.php" method="post">
	<input type="hidden" name="login" value="<?php echo $login; ?>">
	<table>
		<tr>
			<th>Zapisz</th>
			<th>Kod</th>
			<th>Nazwa</th>
			<th>Opis</th>
		</tr>

<?php

			for($i = 0; $i < $n; $i++) {

				$row = pg_fetch_array($result, $i);
				echo "<tr>\n";
				if($i == 0) { // jeśli pierwszy wiersz

					echo "<td><input type=\"radio\" name=\"kod\" value=\"", $row["kod"], "\" checked=\"checked\"></td>\n";

				}
				else { // niepierwszy wiersz

					echo "<td><input type=\"radio\" name=\"kod\" value=\"", $row["kod"], "\"></td>\n";

				}
				echo "<td>", $row["kod"], "</td>\n";
				echo "<td>", $row["nazwa"], "</td>\n";
				echo "<td>", $row["opis"], "</td>\n";
				echo "</tr>\n";

			}

?>

		<tr>
			<td colspan="5"><input type="submit" value="Dalej"></td>
		</tr>
	</table>
</form>

<?php
		 }
?>
